import pandas as pd
import numpy as np
from collections import Counter
import string

def generate_data(rows,columns):
    '''
    按照指定行列数生成随机字母数据，并将其转换为DataFrame类型的变量
    :param:row:指定的行数
    :param:columns:指定列数
    :return:df
    '''
    columns_name = [f'col_{i+1}' for i in range(columns)]
    letters = string.ascii_letters
    random_data = np.random.choice(list(letters),size=(rows,columns))
    return pd.DataFrame(random_data,columns=columns_name)

## 统计元素
def count_analysis(df):
    all_elements = df.values.flatten()
    return Counter(all_elements)

def data_save(elements_data):
    data = pd.DataFrame.from_dict(elements_data,orient='index',columns=['count'])
    data.index_name = 'elements'
    file_path = './weblog/Output/result.csv'
    data.to_csv(file_path)

    
def main():
    ROW = 40
    COL = 12
    df = generate_data(ROW,COL)
    element_data = count_analysis(df)
    data_save(element_data)

if __name__ == '__main__':
    main()